package com.solome.util;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.FreeTypeFontLoaderParameter;
import com.badlogic.gdx.utils.ObjectSet;
import com.solome.base.Logger;
import com.solome.device.IConfig;

/**
 * 
 * 加载TTF
 * 
 * @author soells
 *
 */
public class FontFactory {
	private static FontFactory instance = null;// = new FontFactory();
	public final String RUSSIAN_FONT_NAME = IConfig.FONT_RES;
	private final AssetManager assetManager = new AssetManager();

	private final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			+ "abcdefghijklmnopqrstuvwxyz\n1234567890"
			+ "\"!`?'.,;:()[]{}<>|/@\\^$-%+=#_&~*";

	public static synchronized FontFactory getInstance() {
		if (instance == null) {
			instance = new FontFactory();
		}
		return instance;
	}

	public FontFactory() {
		FileHandleResolver resolver = new InternalFileHandleResolver();
		assetManager.setLoader(FreeTypeFontGenerator.class,
				new FreeTypeFontGeneratorLoader(resolver));
		assetManager.setLoader(BitmapFont.class, ".ttf",
				new FreetypeFontLoader(resolver));
		FreeTypeFontGenerator.setMaxTextureSize(128);
		// FontParameter fontParameter = new FontParameter();
		// fontParameter.setBorderColor(Color.BLACK);
	}

	// ============level up================//e

	public synchronized BitmapFont init(String font, int size) {
		return this.init(font, size, 0, null, false, 0, 0, null, false);
	}

	public synchronized BitmapFont init(String font, int size,
			float borderWidth, Color borderColor, boolean borderStraight,
			int shadowOffsetX, int shadowOffsetY, Color shadowColor,
			boolean flip) {
		return this.init(RUSSIAN_FONT_NAME, font, size, borderWidth,
				borderColor, borderStraight, shadowOffsetX, shadowOffsetY,
				shadowColor, flip);
	}

	public synchronized BitmapFont init(final String fileName, String font,
			int size, float borderWidth, Color borderColor,
			boolean borderStraight, int shadowOffsetX, int shadowOffsetY,
			Color shadowColor, boolean flip) {
		String assetKey = size + ".ttf";
		if (!assetManager.isLoaded(assetKey, BitmapFont.class)) {
			Logger.error(this, "..............加载处理=" + assetKey);
			FreeTypeFontLoaderParameter parameter = new FreeTypeFontLoaderParameter();
			parameter.fontParameters.borderColor = borderColor;
			parameter.fontParameters.borderWidth = borderWidth;
			parameter.fontParameters.borderStraight = borderStraight;
			parameter.fontParameters.shadowOffsetX = shadowOffsetX;
			parameter.fontParameters.shadowOffsetY = shadowOffsetY;
			parameter.fontParameters.shadowColor = shadowColor;
			parameter.fontParameters.flip = flip;
			parameter.fontParameters.incremental = true;
			// parameter.fontParameters.genMipMaps = true;
			parameter.fontParameters.size = size;
			parameter.fontParameters.minFilter = TextureFilter.Linear;
			parameter.fontParameters.magFilter = TextureFilter.Linear;
			parameter.fontParameters.characters += this
					.removerepeatedchar(font) + CHARACTERS;//
			parameter.fontFileName = fileName;
			assetManager.load(assetKey, BitmapFont.class, parameter);
			assetManager.finishLoading();
			parameter = null;
		}
		return this.assetManager.get(assetKey);
	}

	public synchronized BitmapFont getFont(int size) {
		String assetKey = size + ".ttf";
		if (!assetManager.isLoaded(assetKey, BitmapFont.class)) {
			return this.init("", size);
		}
		return this.assetManager.get(assetKey);
	}

	public int size() {
		return this.assetManager.getLoadedAssets();
	}

	/**
	 * 获取不重复字符串
	 * 
	 * @param characters
	 * @return
	 */
	public String removerepeatedchar(String characters) {
		char[] cs = characters.toCharArray();
		ObjectSet<Character> set = new ObjectSet<Character>();
		StringBuilder sb = new StringBuilder();
		for (char c : cs) {
			if (set.add(c)) {
				sb.append(c);
			}
		}
		set.clear();
		return sb.toString();

	}

	/**
	 * Dispose resources
	 */
	public void dispose() {
		assetManager.dispose();
	}
}
